package com.kunpan.admin.controller;


import com.github.pagehelper.PageHelper;
import com.kunpan.admin.common.domain.AjaxResult;
import com.kunpan.admin.entity.FileShare;
import com.kunpan.admin.entity.dto.FileShareDto;
import com.kunpan.admin.exception.file.ValidityException;
import com.kunpan.admin.service.FileShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/file/share")
public class FIleShareController {
    @Autowired
    private FileShareService fileShareService;


    /**
     * 获取用户文件分享列表
     */
    @GetMapping("/list")
    public AjaxResult getFileShareList(FileShareDto fileShareDto) {
        //开启分页查询
        PageHelper.startPage(fileShareDto.getPageNum(), fileShareDto.getPageSize());
        return fileShareService.getFileShareList(fileShareDto);
    }


    /**
     * 根据文件路径和提取码获取文件分享信息
     */
    @GetMapping("/{pathParam}")
    public AjaxResult getFileShareInfo(@PathVariable("pathParam") String pathParam,
                                       @Param("code") String code) {
        return fileShareService.getFileShareInfo(pathParam, code);
    }

    /**
     * 根据文件id获取文件分享信息
     */
    @GetMapping("/{shareId}/{pathParam}")
    public AjaxResult getFileShareInfoByFileId(@PathVariable("pathParam") String pathParam, @PathVariable("shareId") String shareId) {
        return fileShareService.getFileShareInfoById(pathParam, shareId);
    }

    /**
     * 分享文件
     */
    @PostMapping("/add")
    public AjaxResult addFileShare(@RequestBody FileShareDto fileShareDto) {
        try {
            return fileShareService.addFileShare(fileShareDto);
        } catch (ValidityException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改分享的文件
     */
    @PutMapping("/update")
    public AjaxResult updateFileShare(@RequestBody FileShare fileShare) {
        try {
            return fileShareService.updateFileShare(fileShare);
        } catch (ValidityException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据文件id取消分享
     */
    @DeleteMapping("/cancel/{ids}")
    public AjaxResult deleteFileShare(@PathVariable("ids") String[] ids) {
        try {
            return fileShareService.deleteFileShareByIds(ids);
        } catch (ValidityException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 将分享的文件添加到自己的网盘
     */
    @PostMapping("/add/pan")
    public AjaxResult addShareFile(@RequestBody FileShareDto fileShareDto) {
        try {
            return fileShareService.addShareFile(fileShareDto);
        } catch (ValidityException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
